<template>
  <div class="index">
    <div class="index_nav">
      <div class="nav">
        <div>用户：{{username}}</div>
        <div class="heng"></div>
        <div @click="handleput1()">修改密码</div>
        <div class="heng"></div>
        <div @click="romve">注销</div>
      </div>
    </div>
    <div class="index_main" v-if="menu.legth!=0">
      <div class="main_l">
        <el-menu
          :default-active="num"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
        >
          <el-submenu :index="item.id+''" v-for="(item,index) in menu" :key="index">
            <template slot="title">
              <i :class="item.ioc"></i>
              <span>{{item.menu}}</span>
            </template>
            <el-menu-item
              :index="item1.id+''"
              v-for="(item1,index) in item.data"
              :key="index"
              @click="push(item1)"
            >{{item1.menu}}</el-menu-item>
          </el-submenu>
        </el-menu>
      </div>
      <div class="main_r" v-if="menu.legth!=0">
        <router-view />
      </div>
    </div>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="form" :label-width="formLabelWidth">
        <el-form-item label="用户名" prop="user_name">
          <el-input v-model="form.user_name"></el-input>
        </el-form-item>
        <el-form-item label="旧密码" :label-width="formLabelWidth">
          <el-input v-model="form.old_password" autocomplete="off" placeholder="请输入旧密码"></el-input>
        </el-form-item>
        <el-form-item prop="phones" label="新密码" :label-width="formLabelWidth">
          <el-input
            oninput="value=value.replace(/[\W]/g,'')"
            maxlength="20"
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item prop="phones" label="新密码" :label-width="formLabelWidth">
          <el-input
            oninput="value=value.replace(/[\W]/g,'')"
            maxlength="20"
            type="password"
            v-model="form.password1"
            placeholder="请再次输入密码"
            @blur="Verification"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="queding">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { forget, log_out } from "../axios/api";
export default {
  data() {
    return {
      menu: [],
      num: "",
      formLabelWidth: "80px",
      dialogFormVisible: false,
      username: "",
      form: {
        mima: "",
        password: "",
        old_password: "",
        name: "",
        token: sessionStorage.getItem("Token1"),
      },
      rules: {
        user_name: [
          { required: true, message: "请填写用户名", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.menu = JSON.parse(sessionStorage.getItem("menu"));
    if (sessionStorage.getItem("id")) {
      console.log(123);
      this.num = sessionStorage.getItem("id") + "";
    } else {
      this.num = this.menu[0].data["0"].id + "";
    }

    this.username = sessionStorage.getItem("user_name");
    console.log(this.menu);
    console.log(this.num);
  },
  components: {},

  methods: {
    push(e) {
      console.log(e);
      sessionStorage.setItem("id", e.id);
      this.$router.push({ path: e.menu_url });
    },
    Verification() {
      if (this.form.password1.length >= 6) {
        if (this.form.password1 != this.form.password) {
          this.$message({
            message: "两次密码不一致请重新输入",
            type: "error",
          });
          this.form.password1 = "";
        }
      } else {
        this.form.password = "";
        this.form.password1 = "";
        this.$message({
          message: "密码最低为6位数",
          type: "error",
        });
      }
    },
    romve() {
      let params = {
        token: sessionStorage.getItem("Token1"),
      };
      log_out(params).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: "退出成功",
            type: "success",
          });
          sessionStorage.removeItem("Token1");
          sessionStorage.removeItem("id");
          this.$router.push({ path: "/login" });
        }
      });
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    handleput1(e) {
      this.dialogFormVisible = true;
      this.form = {};
    },
    queding() {
      this.form.token = sessionStorage.getItem("Token1");
      let params = this.form;
      console.log(params);
      forget(params).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.dialogFormVisible = false;
        }
      });
    },
  },
};
</script>

<style scoped>
.index {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.index {
  display: flex;
  flex-direction: column;
}
.index_nav {
  width: 100%;
  background: url("../assets/static/img/top.jpg") no-repeat 100%;
  background-size: 100% 100%;
  /* background-size: cover; */
  height: 300px;
}
.index_main {
  height: 75%;
  width: 100%;
  /* display: flex; */
}
.main_l {
  width: 250px;
  height: 100%;
  float: left;
}
.main_r {
  width: calc(100% - 250px);
  height: 100%;
  background: #eee;
  box-sizing: border-box;
  padding: 10px;
  float: left;
}
.index_nav {
  position: relative;
}
.nav {
  width: 440px;
  position: absolute;
  right: 40px;
  bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  color: #fff;
  font-size: 20px;
}
.heng {
  height: 15px;
  width: 2px;
  background-color: #fff;
}
</style>